<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .customization_toolBar {
            border: none;
        }

        .customization_toolBar .toolBar {
            border: none;
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            height: 20px;
            margin: 15px 0px 0px 0px;
        }

        .customization_toolBar .toolBar .left_tool_div {
            flex-basis: 70%;
            height: 100%;
            display: flex;
            flex-direction: row;
            align-items: center;
        }

        .left_tool_div > * + * {
            margin-left: 4%;
        }

        .customization_toolBar .toolBar .right_tool_div {
            flex-basis: 30%;
            height: 100%;
            display: flex;
            flex-direction: row;
            justify-content: flex-end;
            align-items: center
        }

        .right_tool_div > * + * {
            margin-left: 4%;
        }

        .text-show-btn {
            border: none;
            font-size: 14px;
            color: #666666;
        }

        .text-show-btn:hover {
            color: #666666;
        }

        #dataPreviewTable {
            width: 100%;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="customization_toolBar">
            <div class="toolBar">
                <div class="left_tool_div">
                    <div>
                        <button class="layui-btn layui-btn-primary layui-btn-sm data-add-btn text-show-btn"
                                onclick="stateSearch('')">
                            累计销量<label id="ALLSALESQUANTITY" style="color:#ff0004;"> 5</label>
                            件
                        </button>
                    </div>
                    <div>
                        <button class="layui-btn layui-btn-primary layui-btn-sm data-add-btn text-show-btn"
                                onclick="stateSearch('已付款')">
                            待发货<label id="PAYMENT" style="color:#ff0004;text-decoration: underline;">(5)</label>
                        </button>
                    </div>
                    <div>
                        <button class="layui-btn layui-btn-primary layui-btn-sm data-add-btn text-show-btn"
                                onclick="stateSearch('已发货')">
                            已发货<label id="SHIPPED" style="color:#ffd200;text-decoration: underline;">(5)</label>
                        </button>
                    </div>
                    <div>
                        <button class="layui-btn layui-btn-primary layui-btn-sm data-add-btn text-show-btn"
                                onclick="stateSearch('已完成')">
                            已完成<label id="COMPLETED" style="color:#00ff00;text-decoration: underline;">(5)</label>
                        </button>
                    </div>
                    <div>
                        <button class="layui-btn layui-btn-primary layui-btn-sm data-add-btn text-show-btn"
                                onclick="stateSearch('已退款')">
                            退款<label id="REFUNDED" style="color:#616161;text-decoration: underline;">(5)</label>
                        </button>
                    </div>
                </div>
                <div class="right_tool_div">
                    <div>
                        <input type="text" name="dateRange" id="dateRange" class="layui-input"
                               placeholder="请选择时间区间">
                    </div>
                    <div>
                        <button type="submit" class="layui-btn layui-btn-primary dataSearch"><i class="layui-icon"></i>
                            搜 索
                        </button>
                    </div>
                    <div>
                        <button class="layui-btn layui-btn-sm layui-btn-warm orderExportExcel">
                            导出
                        </button>
                    </div>
                </div>
            </div>

        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script>
    let goodsNo;
    window.onload = function () {
        goodsNo = GetQueryString("goodsNo");
        if (isNotNull(goodsNo)) {
            init(goodsNo);
        } else {
            layer.msg('数据异常，请重试');
            setTimeout(function () {
                let iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);
            }, 1500);
        }
    }

    function init(goodsNo) {
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;
            table.render({
                elem: '#currentTableId',
                url: '/trade/order/record/page/list',
                where: {
                    goodsNo: goodsNo
                },
                method: 'GET',
                contentType: 'application/x-www-form-urlencoded',
                parseData: function (res) { //res 即为原始返回的数据
                    let data = res.data;
                    // 处理数据
                    let orderStatusCount = data.orderStatusCount;
                    if (orderStatusCount) {
                        const myMap = new Map(Object.entries(orderStatusCount));
                        // 更新统计数据
                        myMap.forEach((value, key) => {
                            let num = parseFloat(value)
                            let id = '#' + key
                            $(id).text('(' + Math.floor(num) + ')');
                        });
                    }
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": data.total, //解析数据长度
                        "data": data.list //解析数据列表
                    };
                },
                toolbar: false,
                cols: [[
                    {type: "checkbox", fixed: 'left'},
                    {
                        field: 'numbering', title: '序号', width: 60, align: 'center', templet: function (d) {
                            return d.LAY_INDEX; //返回序号
                        }
                    },
                    {field: 'id', title: '订单id', hide: true, sort: true, align: 'center', minWidth: 150},
                    {
                        field: 'orderStatus',
                        title: '订单状态',
                        sort: true,
                        align: 'center',
                        minWidth: 150,
                        templet: function (d) {
                            if (d.orderStatus === '已付款') {
                                return '<span style="color:#ff0000;">待发货</span>'
                            } else if (d.orderStatus === '已发货') {
                                return '<span style="color:#ffc40f;">已发货</span>'
                            } else if (d.orderStatus === '已完成') {
                                return '<span style="color:#319b62;">已完成</span>'
                            } else if (d.orderStatus === '已退款') {
                                return '<span style="color:#b1b1b1;">已退款</span>'
                            }
                        }
                    },
                    {field: 'joinTime', title: '交易时间', sort: true, align: 'center', minWidth: 200},
                    {field: 'orderId', title: '订单编号', sort: true, align: 'center', minWidth: 200},
                    {field: 'buyQuantity', title: '购买数量', sort: true, align: 'center', minWidth: 100},
                    {field: 'realSupplyPrice', title: '实际供货价', sort: true, align: 'center', minWidth: 100},
                    {field: 'payAmount', title: '实付金额', sort: true, align: 'center', minWidth: 120},
                    {field: 'consigneeName', title: '收货人', sort: true, align: 'center', minWidth: 150},
                    {field: 'consigneePhoneNum', title: '手机号', sort: true, align: 'center', minWidth: 150},
                    {field: 'consigneeAddress', title: '收货地址', sort: true, align: 'center', minWidth: 300},
                    {field: 'partnerName', title: '消费者', sort: true, align: 'center', minWidth: 120},
                    {
                        field: 'initiatorName',
                        title: '代理服务商',
                        sort: true,
                        align: 'center',
                        minWidth: 120,
                        templet: function (d) {
                            if (d.onlineProvider) {
                                return '<span>' + d.onlineProvider?.nickName + '（NO.' + d.onlineProvider?.providerId + '） </span>'
                            } else {
                                return '未注册'
                            }
                        }
                    },
                    {
                        field: 'providerName',
                        title: '选址服务商',
                        sort: true,
                        align: 'center',
                        minWidth: 120,
                        templet: function (d) {
                            if (d.offlineProvider) {
                                return '<span>' + d.offlineProvider?.nickName + '（NO.' + d.offlineProvider?.providerId + '） </span>'
                            } else {
                                return '未注册'
                            }
                        }
                    },
                ]],
                limits: [50, 100, 150, 200],
                limit: 50,
                page: true,
                request: {
                    pageName: 'pageNum',
                    limitName: 'pageSize'
                },
                response: { //定义后端 json 格式，详细参见官方文档
                    statusName: 'code', //状态字段名称
                    statusCode: '200', //状态字段成功值
                    msgName: 'msg', //消息字段
                    countName: 'count', //总数字段
                    dataName: 'data' //数据字段
                },
            });

        });
    }

    layui.use(['form', 'table', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#dateRange', //指定元素
            max: 0, // 限制最大值为当前日期
            format: 'yyyy-MM-dd', //日期格式
            type: 'date',
            range: true, //开启范围选择
        });


        //监听搜索选择
        $('.dataSearch').click(function () {
            const formDate = {
                goodsNo: goodsNo,
                dateRange: getDateRangeJsonStr()
            }
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: formDate
            }, 'data');
        })

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {

        });

        // 监听导出按钮
        $('.orderExportExcel').click(function () {
            let checkStatus = table.checkStatus('currentTableId');
            const formDate = {
                goodsNo: goodsNo,
                dateRange: getDateRangeJsonStr()
            }
            let data = {
                batchIds: [],
                batchType: 0,
                queryVO: formDate,
            }
            if (checkStatus.isAll) {
                data.batchType = 1;
            } else {
                let dataList = checkStatus.data;
                if (dataList.length > 0) {
                    let ids = [];
                    dataList.forEach(function (item) {
                        ids.push(item.id)
                    });
                    data.batchIds = ids;
                } else {
                    data.batchType = 1;
                }
            }
            downloadExcel('/trade/order/export/excel', data, '订单记录列表.xlsx', 'POST')
        });

    });

    /**
     * 重载表格数据
     */
    function reloadTable() {
        layui.use(['table'], function () {
            let table = layui.table;
            table.reload('currentTableId', {
                url: '/trade/order/record/page/list',
            });
        })
    }

    function stateSearch(state) {
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                table = layui.table;
            const formDate = {
                goodsNo: goodsNo,
                dateRange: getDateRangeJsonStr(),
                orderStatus: state
            }
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: formDate
            }, 'data');
        });
    }

    // 获取时间查询区间
    function getDateRangeJsonStr() {
        let dateRangeStr = $('#dateRange').val();
        if (!isNotNull(dateRangeStr)) {
            return "{}";
        }
        let dataRangeList = dateRangeStr.split(` - `)
        let dataRange = {}
        if (dataRangeList.length === 2) {
            dataRange = {
                'startDate': dataRangeList[0],
                'endDate': dataRangeList[1]
            }
        }
        return JSON.stringify(dataRange)
    }

</script>

</body>
</html>